package iconSwingStates.Applications_Exemple.Paint;

import iconSwingStates.StateMachine.SMIconStateMachine;
import iconSwingStates.Transitions.TSlot;
import iconSwingStates.Transitions.TTyped;

import javax.swing.JFrame;

import fr.lri.swingstates.canvas.CPolyLine;
import fr.lri.swingstates.canvas.Canvas;
import fr.lri.swingstates.sm.State;
import fr.lri.swingstates.sm.Transition;

/**
 * This class is a SwingStateMachine adapted to ICon. It's the machine's Tool of the painting Program. (Color, brush, forme)
 * @author uribe - gaget
 * 
 */
public class SMSelectionTool extends SMIconStateMachine{
	State Tools;
	CPolyLine line;
	private int forme=0;
	private int brushWidth=0;
	private final int WIDTH_MAX=15;
	private final int NBFORME=3;
	private static SMDraw sm;
	private static SMPalette palette;

	/**
	 * Here our sate machine needs the other states machines cause interacts directly with their parameters. 
	 * @param name
	 * @param frame
	 * @param c
	 * @param sm
	 * @param palette
	 */
	public SMSelectionTool(String name, JFrame frame,Canvas c,SMDraw sm,SMPalette palette) {
		super(name,frame,c);
		/*
		 * Only one state, but composed with TSlot transitions. TSlot is not linked with any pointer, but it's a slot of our device.
		 */
		Tools = new State() {
			/*Brush size transition*/
			Transition incr = new TSlot("brush_size",INT, false){
				@Override
				public void action() {
					int wheel=getEvent().getIntValues().get(0); 
					if (brushWidth<WIDTH_MAX && wheel==1){
						brushWidth+=wheel;     }
					if (brushWidth>0 && wheel==-1){
						brushWidth+=wheel;}
					SMSelectionTool.sm.setBrush(brushWidth);
				}

			};
			/*red color changed transition*/
			Transition red = new TSlot("mod_red",INT, false){
				public void action(){
					int r=SMSelectionTool.palette.getRed();
					int rr=(Integer) getEvent().getIntValues().get(0);
					if (rr==0)
						r=SMSelectionTool.sm.getR()-1;
					else
						r=rr +SMSelectionTool.sm.getR();
					SMSelectionTool.sm.setR(r);

				}
			};
			/*green color changed transition*/
			Transition green = new TSlot("mod_green",INT, false){
				public void action(){
					int g=SMSelectionTool.palette.getGreen();
					int gg=(Integer) getEvent().getIntValues().get(0);
					if (gg==0)
						g=SMSelectionTool.sm.getG()-1;
					else
						g=gg +SMSelectionTool.sm.getG();
					SMSelectionTool.sm.setG(g);
				}
			};
			/*blue color changed transition*/
			Transition blue = new TSlot("mod_blue",INT, false){
				public void action(){
					int b=SMSelectionTool.palette.getBlue();
					int bb=(Integer) getEvent().getIntValues().get(0);
					if (bb==0)
						b=SMSelectionTool.sm.getB()-1;
					else
						b=bb +SMSelectionTool.sm.getB();
					SMSelectionTool.sm.setB(b);
				}
			};
			/*alpha color changed transition*/
			Transition alpha = new TSlot("mod_alpha",INT, false){
				public void action(){
					int a=SMSelectionTool.palette.getAlpha();
					int aa=(Integer) getEvent().getIntValues().get(0);
					if (aa==0)
						a=SMSelectionTool.sm.getA()-1;
					else
						a=aa +SMSelectionTool.sm.getA();
					SMSelectionTool.sm.setA(a);
				}
			};
			/*forme changed transition*/
			Transition forme_c = new TTyped("forme",BOOLEAN, false){
				@Override
				public void action() {
					forme=SMPalette.type;
					forme++;
					forme=forme % NBFORME;
					SMSelectionTool.sm.setForm(forme);
					SMPalette.type=forme;
					SMSelectionTool.palette.majType();
				}
			};      
		};
	}
}